package com.example.gameandroid;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class GameBD extends SQLiteOpenHelper {
	
	static final String tableHiscore = "tableHiscore";
    static final String fHighscore = "fHighscore";

	public GameBD(Context context, String name, CursorFactory factory,int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS "+tableHiscore+" (" +
                fHighscore + " INTEGER  " +
                ")");
		
		 ContentValues cv = new ContentValues();
         cv.put(fHighscore, 0);
         db.insert(tableHiscore, null, cv);
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		System.out.println("onUpgradeDB()");

		db.execSQL("DROP TABLE IF EXISTS " + tableHiscore);
		onCreate(db);
	}

	public int getHighscore() {
		 SQLiteDatabase myDB = this.getReadableDatabase();
         String[] mySearch = new String[]{String.valueOf(0)};
         Cursor myCursor = myDB.rawQuery("SELECT "+ fHighscore +" FROM "+ tableHiscore +"", mySearch);
         myCursor.moveToFirst();
         int index = myCursor.getColumnIndex(fHighscore);
         int myAnswer = myCursor.getInt(index);
         myCursor.close();
		
		return myAnswer;
	}

	public void setHighscore(int highscore) {  
		SQLiteDatabase myDB = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		
		cv.put(fHighscore, highscore);
		
		myDB.update(tableHiscore, cv, "id=?", new String[] { String.valueOf(1) });
	}
}
